//
//  ClubAdminSettingViewController.swift
//  77Club
//
//  Created by Apple on 2024/12/30.
//

import UIKit

class ClubAdminSettingViewController: ClubBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("管理员权限").color(.black).font(16.font_semiBold)
        .alignment(.center).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubForumAdminCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 64.0.Scale
        return tableView
    }()
    
    private var dataSource = [ClubForumAdminModel]()
    
    var communityId = ""
    var user_id: String = "" {
        didSet{
            loadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            tableView
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        tableView.fillContainer().top(.navi_height)
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }

    private func loadData(){
        Task{
            let res = await self.network.request(.adminPerm(gid: communityId, user_id: user_id), type: ClubForumAdminPermModel.self)
            if let model = res.model, res.isSuccess {
                
                let noticeM = ClubForumAdminModel(name: "论坛公告", is_sel: model.perm_notice == 1)
                let banM = ClubForumAdminModel(name: "人员禁言", is_sel: model.perm_ban == 1)
                let dingM = ClubForumAdminModel(name: "帖子置顶", is_sel: model.perm_top == 1)
                let postM = ClubForumAdminModel(name: "帖子上下架", is_sel: model.perm_off == 1)
                let followM = ClubForumAdminModel(name: "关注设置", is_sel: model.perm_guanzhu == 1)
                
                dataSource = [noticeM, banM, dingM, postM, followM]
                tableView.reloadData()
            }
        }
    }
}

extension ClubAdminSettingViewController: UITableViewDelegate, UITableViewDataSource  {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubForumAdminCell", for: indexPath) as! ClubForumAdminCell
        cell.model = dataSource[indexPath.row]
        cell.updateAdminPerm = { type in
            self.editAdminPerm(row: indexPath.row, type: type)
        }
        return cell
    }
    
    private func editAdminPerm(row: Int, type: String){
        
        var field = "perm_notice"
        if row == 1 {
            field = "perm_ban"
        }else if row == 2 {
            field = "perm_top"
        }else if row == 3 {
            field = "perm_off"
        }else if row == 4 {
            field = "perm_guanzhu"
        }
        Task{
            ToastUtil.show()
            let models = await network.requestSpecial(.editAdminPerm(gid: communityId, user_id: user_id, field: field, value: type))
            ToastUtil.dismiss()
            if !models.isSuccess {
                models.errorMessage?.toast_error
            }
        }
    }
}


class ClubForumAdminCell: UITableViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(8.0.Scale).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#323233").font(15.font_medium).end()
    private let turnButton = c_make_ex(UIButton.self)
        .image("Square_setting_off").image("Square_setting_on",state: .selected).end()

    var model: ClubForumAdminModel?{
        didSet{
            guard let md = model else { return }
            
            nameLabel.text = md.name
            turnButton.isSelected = md.is_sel
        }
    }
    
    var updateAdminPerm: ((_ type: String) -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            bgView.subviews{
                nameLabel
                turnButton
            }
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(8.0.Scale).bottom(4.0.Scale).height(52.0.Scale)
        nameLabel.leading(12.0.Scale).centerVertically()
        turnButton.trailing(12.0.Scale).height(32.0.Scale).width(52.0.Scale).centerVertically()
        
        turnButton.addTarget(self, action: #selector(clickTurnButton), for: .touchUpInside)
    }
    
    
    @objc private func clickTurnButton(){
        turnButton.isSelected = !turnButton.isSelected
        updateAdminPerm?(turnButton.isSelected ? "1" : "0")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
